/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include <memory>
#include <string>
#include <tuple>
#include <vector>

#include "DBManager/IRepository/Input/Property/IPBEAMLRepository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace PBEAML
{
    struct IDENTITY
    {
        SG::DataStructure::Common::I64 PID;
        SG::DataStructure::Common::I64 MID;
        char                           GROUP[8];
        char                           TYPE[8];
        SG::DataStructure::Common::I64 SECTION_POS;
        SG::DataStructure::Common::I64 SECTION_LEN;
        SG::DataStructure::Common::I64 DOMAIN_ID;
    };

    struct SECTION
    {
        SG::DataStructure::Common::Real SO;
        SG::DataStructure::Common::Real RDIST;
        SG::DataStructure::Common::I64  DIMS_POS;
        SG::DataStructure::Common::I64  DIMS_LEN;
        SG::DataStructure::Common::Real NSM;
    };

};  // namespace PBEAML
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::PBEAML::IDENTITY> ()
{
    HighFive::CompoundType compoundType = {
        { "PID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "MID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "GROUP", HighFive::create_datatype<char[8]> (), 1, true },
        { "TYPE", HighFive::create_datatype<char[8]> (), 1, true },
        { "SECTION_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "SECTION_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::PBEAML::SECTION> ()
{
    HighFive::CompoundType compoundType = {
        { "SO", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "RDIST", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "DIMS_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "DIMS_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "NSM", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
    };
    return compoundType;
}

SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class PBEAMLRepository : public IPBEAMLRepository, public MainlyRepositoryBase<SG::DataStructure::FEM::PBEAMLData>
{
  public:
    explicit PBEAMLRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SECTION,
        DIMS
    };

    /**
     * @brief   : 初始化表数据
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t index) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief    Get the All Data object
     *
     * @return
     * @author   wxc
     * @date     2024.12.25
     */
    std::vector<SG::DataStructure::FEM::PBEAMLData> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<SG::DataStructure::FEM::PBEAMLData>&   own,
                       std::vector<PBEAML::IDENTITY>& _OUT                IDENTITYData,
                       std::vector<PBEAML::SECTION>& _OUT                 SECTIONData,
                       std::vector<SG::DataStructure::Common::Real>& _OUT DIMSData,
                       bool                                               isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITY
     * @param   : INFO
     * @param   : own
     */
    void NastranToOwn (std::vector<PBEAML::IDENTITY>&                        IDENTITYData,
                       std::vector<PBEAML::SECTION>&                         SECTIONData,
                       std::vector<SG::DataStructure::Common::Real>&         DIMSData,
                       std::vector<SG::DataStructure::FEM::PBEAMLData>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
